package com.hddf.project.service;

import java.util.List;

import org.hibernate.Session;
import org.kommii.mysql_conn_pool.SessionProvider;

import com.hddf.project.base.BaseService;
import com.hddf.project.dao.CommentDao;
import com.hddf.project.impl.CommentDaoImpl;
import com.hddf.project.pojos.Comment;
import com.hddf.project.pojos.Project;
import com.hddf.project.pojos.User;
import com.hddf.project.util.O;

public class CommentService extends BaseService {

	private static CommentService cts = null;
	private static CommentDao ctd = null;

	static {
		if (null == cts) {
			cts = new CommentService();
		}
		if (null == ctd) {
			ctd = new CommentDaoImpl();
		}
	}

	public static CommentService getInService() {
		return cts;
	}

	/**
	 * 添加批示
	 * 
	 * @param userId
	 * @param content
	 * @throws Exception
	 */
	public Comment addComment(Integer userId, Integer pid, String content) throws Exception {
		Comment c = null;
		Project p = (Project) super.getObjectById(pid, Project.class);
		if (null != p) {
			p.setComment((short) 2);
			p.setUpdate_time(O.getTimestamp3());
			super.updateObject(p);

			c = new Comment();
			c.setUserId(userId);
			c.setProjectId(pid);
			c.setContent(content);
			c.setInputTime(O.getTimestamp3());
			c.setUpdateTime(O.getTimestamp3());
			super.addObject(c);
		}
		return c;
	}

	/**
	 * 修改批示
	 * 
	 * @param cid
	 * @param content
	 * @throws Exception
	 */
	public void updateComment(Integer cid, String content) throws Exception {
		Comment c = (Comment) super.getObjectById(cid, Comment.class);
		if (null != c && !c.getContent().equals(content)) {
			if (O.ckSt(content)) {// 批示有内容
				c.setContent(content);
				c.setUpdateTime(O.getTimestamp3());
				super.updateObject(c);

				Project p = (Project) super.getObjectById(c.getProjectId(), Project.class);
				if (null != p) {
					p.setUpdate_time(O.getTimestamp3());
					super.updateObject(p);
				}
			} else {// 批示无内容
				super.deleteObject(cid, Comment.class);// 删除批示数据

				this.checkComment(c.getProjectId());
			}
		}
	}

	/**
	 * 修改批示
	 * 
	 * @param cid
	 * @param content
	 * @throws Exception
	 */
	public void delComment(Integer cid) throws Exception {
		Comment c = (Comment) super.getObjectById(cid, Comment.class);
		if (null != c) {
			super.deleteObject(c);// 删除批示数据

			this.checkComment(c.getProjectId());
		}
	}

	/**
	 * 检查批示
	 * 
	 * @param pid
	 */
	private void checkComment(int pid) {
		try {
			int result = this.countComment(pid);// 查看该批示所在项目是否还有其他批示
			if (0 == result) {// 如果没有其他批示
				Project p = (Project) super.getObjectById(pid, Project.class);
				if (null != p) {
					p.setComment((short) 1);
					p.setUpdate_time(O.getTimestamp3());
					super.updateObject(p);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据项目ID查询批示内容
	 * 
	 * @param pid
	 * @return
	 */
	public List<Comment> listComment(Integer pid) {
		List<Comment> clist = null;
		Session s = null;
		try {
			s = SessionProvider.getSession();
			clist = ctd.listComment(pid, s);
			SessionProvider.commitSession(s);
			if (O.ckLt(clist)) {
				for (Comment c : clist) {
					c.setUserName(((User) super.getObjectById(c.getUserId(), User.class)).getUsername());
				}
			}
		} catch (Exception e) {
			SessionProvider.rollback(s);
			e.printStackTrace();
		}
		return clist;
	}

	/**
	 * 根据项目ID查询批示数量
	 * 
	 * @param pid
	 * @return
	 */
	public Integer countComment(Integer pid) {
		Integer result = 0;
		Session s = null;
		try {
			s = SessionProvider.getSession();
			result = ctd.countComment(pid, s);
			SessionProvider.commitSession(s);
		} catch (Exception e) {
			SessionProvider.rollback(s);
			e.printStackTrace();
		}
		return result;
	}

	// /**
	// * 根据项目ID查询批示数量
	// *
	// * @param pid
	// * @return
	// */
	// public Comment commentDetail(Integer pid, Integer userid) {
	// Comment c = null;
	// Session s = null;
	// try {
	// s = SessionProvider.getSession();
	// c = ctd.commentDetail(pid, userid, s);
	// SessionProvider.commitSession(s);
	// } catch (Exception e) {
	// SessionProvider.rollback(s);
	// e.printStackTrace();
	// }
	// return c;
	// }
}
